package com.zxy.supplier_system.server.controller

import com.zxy.supplier_system.server.entity.Authority
import com.zxy.supplier_system.server.entity.Goods
import com.zxy.supplier_system.server.service.DirectSaleOrderService
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import org.springframework.transaction.annotation.Transactional
import org.springframework.web.bind.annotation.*
import java.math.BigDecimal

@RestController
@RequestMapping("directSaleClients/orders/{orderId}/items")
class DirectSaleOrdersItemController(
    private val directSaleOrderService: DirectSaleOrderService
) {

    data class AddDirectSaleOrdersItemRequest(
        val goodsId:Int,
        val amount:Int,
        val price:BigDecimal
    )

    @HasAnyAuthority(Authority.经销_开单)
    @PostMapping
    fun addItem( @PathVariable orderId: Int,@RequestBody addDirectSaleOrdersItemRequest: AddDirectSaleOrdersItemRequest){
        this.directSaleOrderService.addItem(orderId,addDirectSaleOrdersItemRequest)
    }

    @HasAnyAuthority(Authority.经销_开单)
    @DeleteMapping("{goodsId}")
    fun deleteItem(@PathVariable goodsId: Int, @PathVariable orderId: Int){
        this.directSaleOrderService.deleteItem(orderId,goodsId)
    }

    @Transactional
    class UpdateItemAmountRequest(
        val amount: Int
    )

    @HasAnyAuthority(Authority.经销_开单)
    @PatchMapping("{goodsId}/amount")
    fun updateItemAmount(@PathVariable goodsId: Int, @PathVariable orderId: Int,@RequestBody updateItemAmountRequest: UpdateItemAmountRequest){
        return this.directSaleOrderService.updateItemAmount(goodsId,orderId,updateItemAmountRequest)
    }

    @HasAuthority(Authority.经销_查询)
    @GetMapping
    fun queryOrderItemsInOrder(@PathVariable orderId: Int,pageable: Pageable):Page<OrderItemInOrderSummary>{
        return this.directSaleOrderService.queryOrderItemsInOrder(orderId,pageable)
    }

    @HasAnyAuthority(Authority.经销_开单)
    @GetMapping("{goodsId}/exists")
    fun exists(@PathVariable goodsId: Int, @PathVariable orderId: Int): Boolean {
        return this.directSaleOrderService.exists(orderId,goodsId)
    }

    @HasAnyAuthority(Authority.经销_开单)
    @GetMapping("barcodes/{barcode}/exists")
    fun exists(@PathVariable barcode: String, @PathVariable orderId: Int): Boolean {
        return this.directSaleOrderService.exists(orderId,barcode)
    }

    data class OrderItemInOrderSummary(
        val goodsId:Int,
        val name:String,
        val barcode:String,
        val amountInOrder: Int,
        val inStore:Int,
        val boxSpecification:String?
    ){
        companion object{
            fun fromGoodsAndAmount(goods: Goods,amount:Int): OrderItemInOrderSummary {
                return OrderItemInOrderSummary(
                    goods.id,
                    goods.name,
                    goods.barcode,
                    amount,
                    goods.amount,
                    goods.boxSpecification,
                )
            }
        }
    }

    @HasAnyAuthority(Authority.经销_开单)
    @PostMapping("barcodes/{barcode}")
    fun addItem(@PathVariable barcode: String, @PathVariable orderId: Int):OrderItemInOrderSummary{
        return this.directSaleOrderService.addItem(orderId,barcode)
    }

    @HasAnyAuthority(Authority.经销_开单)
    @PostMapping("goods/{goodsId}")
    fun addItem(@PathVariable goodsId: Int, @PathVariable orderId: Int):OrderItemInOrderSummary{
        return this.directSaleOrderService.addItem(orderId,goodsId)
    }

}